<?php
namespace app\api\controller;

use app\common\controller\Common;
use think\facade\Cache;
use think\helper\Time;

class User extends Common
{

    public function add_book () {
        $param = $this->request->only(['token','act_type','novel_id']);

        $request['novel_id'] = $param['novel_id'];
        $request['platform'] = 'app';
        $request['type'] = $param['act_type'];
        $request['token'] = $param['token'];

        //更新收藏排行榜
//        if ($data['type'] == "collect") {
//            $article = model('article')->where('PrimaryId','eq',$data['id'])->cache()->find()->toArray();
//            //获取当前分类
//            $article['category'] = get_category($article['Cid']);
//            add_redis_cid_ranking(get_cut_value($article['PrimaryId']),$article['category']['cid'],"collect");
//        }

        $result = curl_server("api_v1/user_add_book",$request);

        if (!empty($result['data'])) {
            $this->echoJson('',$result['code'],$result['msg']);
        }else {
            $this->echoJson('',$result['code'],$result['msg']);
        }
    }
    /**
     * 用户信息
     */
    public function user_info () {
        $param = $this->request->only(['token']);

        $result = curl_server("api_v1/get_user_info",array('platform' => 'app','token' => $param['token']));
        if (!empty($result['data'])) {
            $data = $result['data'];
            $data['user_id'] = $data['id'];
            $data['code'] = 0;
            $data['msg'] = "信息获取成功";

            unset($data['id']);
            
            $this->echoJson($data);
        }else {
            $this->echoJson(['code' => -1,'msg' => "信息获取失败"]);
        }

    }

    /**
     * 用户登录
     */
    public function user_login () {

        if ($this->request->isPost()) {

            $param = $this->request->only(['username', 'password']);

            if (strlen($param['username']) < 6) {
                $this->echoJson(array('code' => -1,'msg' => "必须大于6个字符"));
            }

            //登录用户
            $login_data = curl_server("api_v1/user_login",array(
                'username' => $param['username'],
                'password' => $param['password'],
                'platform' => "app",
                'last_login_ip' => $this->request->ip(),
            ));

            //登录失败 尝试注册
            if ($login_data['code'] != 0) {
                //检查用户是否被注册
                $check_data = curl_server("api_v1/user_check",array(
                    'type' => "username",
                    'value' => $param['username'],
                    'platform' => "app",
                    'last_login_ip' => $this->request->ip(),
                ));
                //已经被注册过
                if ($check_data['code'] != 0) {
                    $this->echoJson(array('code' => $check_data['code'],'msg' => $check_data['msg']));
                }else {
                    //注册用户
                    $req_data = curl_server("api_v1/user_reg",array(
                        'username' => $param['username'],
                        'password' => $param['password'],
                        'platform' => "app",
                        'last_login_ip' => $this->request->ip(),
                    ));
                    //注册失败
                    if ($req_data['code'] != 0) {
                        $this->echoJson(array('code' => $req_data['code'],'msg' => $req_data['msg']));
                    }else {
                        //登录用户
                        $login_data = curl_server("api_v1/user_login",array(
                            'username' => $param['username'],
                            'password' => $param['password'],
                            'platform' => "app",
                            'last_login_ip' => $this->request->ip(),
                        ));
                        $this->echoJson(['code' => 0,'msg' => '注册成功','token' => $login_data['data']]);
                    }
                }
            }else {
                $this->echoJson(['code' => 0,'msg' => '登录成功','token' => $login_data['data']]);
            }

        }
    }

    /**
     * 删除收藏或者书架
     */
    public function delete_book (){

        if ($this->request->isPost()) {
            $data = $this->request->only(['novel_id','act_type','token']);
            $data['platform'] = 'app';
            $result = curl_server("api_v1/user_delete_book",$data);
            $this->echoJson($result['data'],$result['code'],$result['msg']);
        }

    }

    /**
     * 书架列表
     * @throws
     */
    public function book_shelf () {
        if ($this->request->isPost()) {
            $user_id = $this->request->param('user_id',0);

            //查询置顶数据
            $book_list = model('user_book')->where('Platform','eq',sprintf("app_%d",$this->config['app_id']))->where('ActType','eq',"rack")->field("Platform,ArticleId",true)->whereIn('UserId',array(0,(int)$user_id))->order('Commend','DESC')->order('Sort','DESC')->select()->toArray();

            foreach ($book_list as &$item) {

                unset($item['_id']);

                $novel = model("article")->where('PrimaryId','eq',(int)$item['PrimaryId'])->cache()->find();
                if (!empty($novel)) {
                    $novel = $novel->toArray();
                    $item['Title'] = $novel['Title'];
                    $item['Author'] = $novel['Author'];
                    $item['Image'] = $novel['LocalImage'];
                }else {
                    //如果查询小说为空则删除 书架
                    unset($item);
                }

            }

            return $this->echoJson($book_list);
        }
    }


}
